import dataList from "@/components/dataList";
import editMatchInfo from "./components/edit-match-info";
import lookMatchInfo from "./components/look-match-info";
import importInfo from "./components/import-info";
import { mapGetters } from "vuex";
import {
	recentdemandPage,
	recentdemand,
	getRecentdemand,
	recentdemandEdit
} from "../api/public";
export default {
	name: "planning-index",
	components: { dataList, editMatchInfo, lookMatchInfo, importInfo },
	computed: {
		...mapGetters(["permissions"])
	},
	mounted() {
		this.add_service = this.permissions["recentdemand_edit"];
		this.export_service = this.permissions["recentdemand_export"];
		this.get_service = this.permissions["recentdemand_get"];
		this.streets = this.getDict("streets");
	},
	provide() {
		return {
			initList: this.getListData
		};
	},
	data() {
		return {
			// hxf-0829-表单绑定
			tableData: [],
			// 请求参数
			params: {
				current: 1,
				size: 20,
				streetNames: []
			},
			// 选中的数据
			selectDataArr: [],
			// 分页
			pager: {
				total: 0, // 总页数
				current: 1, // 当前页数
				size: 20 // 每页显示多少条
			},
			// 页面loading
			loading: false,
			filterList: [
				{
					label: "设施类型",
					inputType: "dict",
					dictName: "device_types",
					name: "facilitiesType",
					multiple: true
				},
				{
					label: "街道名称",
					inputType: "dict",
					dictName: "streets",
					name: "streetName",
					multiple: true
				},
				{
					label: "社区名称",
					inputType: "dict",
					dictName: "community_names",
					name: "communityNames",
					multiple: true
				}
			],
			// hxf-0904-权限配置初始值
			add_service: false,
			export_service: false,
			get_service: false,
			searchValue: "",
			streets: []
		};
	},
	beforeMount() {
		this.getListData();
	},
	methods: {
		// hxf-0829-接入分页表单数据
		getListData(params = {}) {
			this.loading = true;
			let requestData = { ...this.params, ...params };
			recentdemandPage(requestData)
				.then(res => {
					const { current, size, total, records } = res.data.data;
					this.tableData = records;
					this.params = requestData;
					this.pager = { total, current, size };
				})
				.finally(() => {
					this.loading = false;
				});
		},
		// 分页搜索
		searchByKeywords(val) {
			//先清空原来的值
			this.params.streetNames = "";
			if (val) {
				//去字典中匹配街道码值
				let streetArr = [];
				this.streets.map(st => {
					if (st.label.indexOf(val) != -1) {
						streetArr.push(st.value);
					}
				});
				if (streetArr && streetArr.length > 0) {
					if (this.params.streetNames) {
						this.params.streetNames.push(...streetArr);
					} else {
						this.params.streetNames = streetArr;
					}
				} else {
					if (this.params.streetNames) {
						this.params.streetNames.push(val);
					} else {
						this.params.streetNames = [val];
					}
				}

				this.getListData({ current: 1 });
			} else {
				this.handleClear();
			}
		},
		// 搜索清空
		handleClear() {
			this.params.streetNames = [];
			this.getListData();
		},
		// hxf-0829-导出
		handleExportTable() {
			this.publicExport(
				"公共服务设施",
				"/api/recentdemand/download",
				this.params
			);
		},
		// hxf-0829-新增
		handleNewlyAdded(row) {
			this.$refs.editMatchInfo.show();
		},
		// hxf-0829-批量导入
		handleBatchImport() {
			this.$router.push({ name: "batch_import" });
		},
		// hxf-0829-预览
		handlePublicPreview() {
			window.open("http://192.168.101.48:9080/map/");
		},
		// hxf-0902-编辑
		handleNewlyEdit(item) {
			getRecentdemand(item.demandId).then(res => {
				this.$refs.editMatchInfo.show(res.data.data);
			});
		},
		// hxf-0902-新增保存
		addData(data) {
			if (!this.loading) {
				this.loading = true;
				recentdemand(data)
					.then(res => {
						this.$message.success("保存成功");
						// this.query();
						this.getListData();
					})
					.finally(() => {
						this.loading = false;
					});
			}
		},
		// hxf-0902-修改保存
		updateData(data) {
			if (!this.loading) {
				this.loading = true;
				recentdemandEdit(data)
					.then(res => {
						this.$message.success("修改成功");
						this.getListData();
					})
					.finally(() => {
						this.loading = false;
					});
			}
		},
		// 菜单点击监听
		handleMenuClick(type) {
			switch (type) {
				case "add":
					this.handleNewlyAdded();
					break;
				case "export":
					this.handleExportTable();
					break;
				case "test":
					this.handleBatchImport();
					break;
				case "test2":
					this.handlePublicPreview();
					break;
				default:
					this.$message(type);
					break;
			}
		}
	}
};
